Class GAid
static var params=[];
static var designW;
static var startTime=0;
static var timeDiff=0;
static var lastDesignTime;
static var designCode;
static var laterCalls=[];
static var nextLaters=[];
static var changedHash;
static var scrollChangedHash;
static var removeCalled=false;
static var qrBox;
static var traceDiv;
static var traceText="";
static var trackIframe;
static var singleId;
static var singleTime=0;
public static function start(){
	window.trace=Me.trace;
	Me.initPlatform();
	IO.setRes("res/",[Me.start2]);
}
static function start2(){
	Me.initTime();
	EvtAid.init();
	Me.initStage();
	if(window.Log) Log.init();
	new Main();
	Me.resetChanged();
}
static function close(){
	if(G.platform==1){
		window.close();
		return;
	}
	if(!window.WeixinJSBridge){
		callLater([Me.close],100);
		return;
	}
	WeixinJSBridge.invoke("closeWindow");
}
static function initPlatform(){
	var agent=navigator.userAgent;
	var platform=navigator.platform;
	if(agent.indexOf("MicroMessenger")>-1){
		G.platform=2;
	}
	else if(!Shell){
		G.platform=3;
	}
	if(platform.indexOf("Win")==0||platform.indexOf("MacIntel")==0){
		G.os=1;
	}
	else if(agent.indexOf("Android")>-1){
		G.os=2;
	}
	else{
		G.os=3;
	}
	G.path=document.URL.replace(/[^\/]+$/g,"");
	G.url=G.path+"index.html";
	G.config=System.config;
}
static function initStage(){
	var meta=document.getElementsByTagName("meta")["viewport"];
	var m=meta.content.match(/width=(\d+)/);
	Me.designW=int(m[1]);
	document.documentElement.style.cssText="width:100%;height:100%";
	var docW=document.documentElement.clientWidth;
	var docH=document.documentElement.clientHeight;
	G.scale=docW/docH<1.57?docW/Me.designW:docH/(Me.designW*1.57);
	if(G.os>1) G.scale=1;
	G.w=int(docW/G.scale);
	G.h=int(docH/G.scale);
	if(G.scale!=1){
		document.documentElement.style[G.toCssKey("TransformOrigin")]="0 0";
		document.documentElement.style[G.toCssKey("Transform")]="scale("+G.scale+","+G.scale+")";
	}
	if(G.os>1&&G.w>G.h) G.h=Me.designW*1.57;
	Me.renderDiv=document.createElement("div");
	document.body.appendChild(Me.renderDiv);
	Me.renderDiv.style.visibility="hidden";

	GAid.trackIframe=document.createElement("iframe");
	document.body.appendChild(GAid.trackIframe);
	GAid.trackIframe.style.display="none";
	GAid.trackIframe.src="js/act.html";
	G.stage=Class.new(Stage);
	for(var i=0;i<Me.params.length;i++){
		var a=Me.params[i];
		if(a[1]==null) a[1]=1;
		if(a[1]=="0"||a[1]=="1") a[1]=int(a[1]);
		G.param[a[0]]=a[1];
	}
}
static function setTitle(text){
	G.title=text;
	if(G.platform==1){
		Shell.ipcRenderer.send("setTitle",text);
	}
	else{
		document.title=text;
	}
	if(G.os==3&&GAid.trackIframe&&GAid.trackIframe.contentWindow.location.href!="about:blank"){
		GAid.trackIframe.contentWindow.location.reload(true);
	}
}
static function initTime(){
	Me.startTime=new Date().valueOf();
	G.now=Me.startTime;
	Me.playTime();
	setInterval(Me.playTime,16);
}
static function adjustTime(time){
	G.time=time;
	Me.timeDiff=G.time-int(new Date().valueOf()/1000);
}
static function createQRLink(){
	if(Me.qrBox) return;
	var os=require("os");
    var ifaces=os.networkInterfaces();
    var ip="127.0.0.1";
    for(var k in ifaces){
    	var a=ifaces[k];
    	for(var i=0;i<a.length;i++){
    		var obj=a[i];
    		if(obj.family=="IPv4"&&!obj.internal){
    			ip=obj.address;
    		}
    	}
    }
	Me.qrBox=G.stage.addChild([
		[{w:"f",h:"f",bg:"#6000"},
			[{w:240,h:240,x:"c",y:"c",bg:"#fff"}]
		]
	]);
	var box=Me.qrBox.children[0];
	box.div.style.padding="20px";
	var qr=new QRCode(box.div,{width:200,height:200});
	qr.makeCode("http://"+ip+":8000");
}
public static function trace(){
	if(!Me.traceDiv){
		Me.traceText="";
		Me.traceDiv=document.createElement("div");
		document.body.appendChild(Me.traceDiv);
		Me.traceDiv.selectable=true;
		Me.traceDiv.style.cssText="position:absolute;width:"+G.w+"px;padding:5px;font-size:24px;-webkit-touch-callout:initial;-webkit-user-select:initial;font-family:Microsoft Yahei,PingFangSC-Light;background:"+G.toColor("#8000");
	}
	var str="";
	for(var i=0;i<arguments.length;i++){
		var v=arguments[i];
		if(str!="") str+=" ";
		if(v==null) v="null";
		str+=v;
	}
	if(str=="") str="　";
	if(Me.traceText!="") Me.traceText+="\n";
	Me.traceText+=str;

	Me.traceDiv.style.overflow="auto";
	Me.traceDiv.style.height="auto";
	Me.traceDiv.innerText=Me.traceText;
	Me.traceDiv.style.color="#ffffff";
	if(Me.traceDiv.offsetHeight>220){
		Me.traceDiv.style.height="220px";
		Me.traceDiv.style.overflow="scroll";
		Me.traceDiv.scrollTop=Me.traceDiv.scrollHeight;
	}
	Me.traceDiv.style.top=(G.h-Me.traceDiv.offsetHeight)+"px";
}
public static function toColor(c){
	if(!c||c=="#none") return "transparent";
	if(c.length<=5){
		var str="#";
		for(var i=1;i<c.length;i++){
			var s=c.charAt(i);
			str+=s+(s=="f"?"f":"0");
		}
		c=str;
	}
	if(c.length==9){
		var s="";
		for(var i=0;i<4;i++){
			var n=(i==3?0:i+1)*2+1;
			s+=int("0x"+c.slice(n,n+2))/(i==3?255:1)+",";
		}
		return "rgba("+s.slice(0,-1)+")";
	}
	return c;
}
public static function toCssKey(k){
	var isFF=navigator.userAgent.indexOf("Firefox")>-1;
	var isIE=window.ActiveXObject!=null;
	var c=k.charCodeAt(0);
	if(isIE){
		if(k=="UserSelect") return "ms"+k;
		if(k=="user-select") return "-ms-"+k;
	}
	if(c>=65&&c<=90){
		if(isFF||isIE) return k.slice(0,1).toLowerCase()+k.slice(1);
		return "webkit"+k;
	}
	else{
		if(isFF||isIE) return k;
		return "-webkit-"+k;
	}
}
public static function newEvent(type,data){
	var evt=document.createEvent("HTMLEvents");
	evt.initEvent(type,true,true);
	evt.enabled=true;
	for(var k in data){
		evt[k]=data[k];
	}
	return evt;
}
public static function addFrameCall(foo){
	for(var i=0;i<Me.frameCalls.length;i++){
		var foo2=Me.frameCalls[i];
		if(is(Function,foo2[0])){
			if(foo[0]==foo2[0]) return;
		}
		else{
			if(foo[0]==foo2[0]&&foo[1]==foo2[1]) return;
		}
	}
	Me.frameCalls.push(foo);
}
public static function removeFrameCall(caller,func){
	for(var i=Me.frameCalls.length-1;i>=0;i--){
		var foo2=Me.frameCalls[i];
		if(is(Function,foo2[0])){
			if(caller==foo2[0]) Me.frameCalls.splice(i,1);
		}
		else{
			if(caller==foo2[0]&&func==foo2[1]) Me.frameCalls.splice(i,1);
		}
	}
}
public static function testDesign(){
	if(!window.Shell||!G.param.design) return;
	if(!Me.designCode){
		Me.lastDesignTime=G.now;
		Me.designCode=IO.readFile("code/"+G.param.design);
		return;
	}
	if(G.now-Me.lastDesignTime<300) return;
	Me.lastDesignTime=G.now;
	var code=IO.readFile("code/"+G.param.design);
	if(code!=Me.designCode){
		Me.designCode=code;
		location.reload(true);
	}
}
public static function resetChanged(){
	if(G.os==3&&G.renderIos){
		G.renderIos=false;
		document.body.removeChild(Me.renderDiv);
		document.body.appendChild(Me.renderDiv);
	}
	for(var i=0;i<Me.laterCalls.length;i++){
		var obj=Me.laterCalls[i];
		if(obj.status==1&&obj.time<=0){
			obj.status=2;
			call(obj.foo);
		}
	}
	for(var i=Me.laterCalls.length-1;i>=0;i--){
		var obj=Me.laterCalls[i];
		if(obj.status==3){
			Me.laterCalls.splice(i,1);
		}
	}
	if(Me.changedHash){
		var hash={};
		for(var k in Me.changedHash){
			hash[k]=Me.changedHash[k];
		}
		Me.changedHash=null;
		for(var k in hash){
			var box=hash[k];
			if(!box.removed) box.resetSelf();
		}
		if(Me.changedHash) Me.resetChanged();
	}
	if(Me.scrollChangedHash){
		for(var k in Me.scrollChangedHash){
			var box=Me.scrollChangedHash[k];
			box.setScroll(box.div.scrollHeight>box.div.clientHeight);
			box.scroll=true;
		}
		Me.scrollChangedHash=null;
	}
}
static function testSingle() {
	if(!window.Shell) return;
	if(!Me.singleId){
		var stat=Shell.fs.statSync(Shell.path+"js/dream-code.js");
		Me.singleId=stat.mtime+"";
	}
	if(G.now-G.singleTime<200) return;
	G.singleTime=G.now;
	var stat=Shell.fs.statSync(Shell.path+"js/dream-code.js");
	var singleId=stat.mtime+"";
	if(Me.singleId!=singleId){
		Me.singleId=singleId;
		G.close();
	}
}
static function playTime(){
	var t=new Date().valueOf();
	var now=t-Me.startTime;
	G.interval=now-G.now;
	G.now=now;
	G.time=int(t/1000)+Me.timeDiff;

	for(var i=0;i<Me.laterCalls.length;i++){
		var obj=Me.laterCalls[i];
		obj.time-=G.interval;
		if(obj.status==2) obj.status=3;
	}
	Me.resetChanged();
	Me.testDesign();
	Me.testSingle();
}